STATIC_LIBS = libcxxcall.a
SHARED_LIBS = libcxxcall.so

HEADERS = abi.hpp cxxcall.hpp
DEPS = $(wildcard sysdeps/*/*.cpp)

OBJS = callout-arg.o callout-call.o callin-arg.o callin-recv.o
LOBJS = $(OBJS:.o=.lo)

TEST_OBJS = $(LOBJS)

-include config.mak

ALL_LIBS = $(STATIC_LIBS) $(SHARED_LIBS)

AR = $(CROSS_COMPILE)ar
RANLIB = $(CROSS_COMPILE)ranlib
CXXFLAGS += $(CXXFLAGS_AUTO)

all: $(ALL_LIBS)

check: $(TEST_OBJS)
	$(CXX) $(CXXFLAGS) tests/test.cpp $(TEST_OBJS) -o tst
	./tst

%.o: %.cpp $(HEADERS) $(DEPS)
	$(CXX) $(CXXFLAGS) -c $< -o $@

%.lo: %.cpp $(HEADERS)
	$(CXX) $(CXXFLAGS) -fPIC -c $< -o $@

callout-call.o: callout-call.cpp $(HEADERS) $(DEPS)
	$(CXX) $(CXXFLAGS_ZERO) $(LDFLAGS) -c $< -o $@

callout-call.lo: callout-call.cpp $(HEADERS) $(DEPS)
	$(CXX) $(CXXFLAGS_ZERO) $(LDFLAGS) -fPIC -c $< -o $@

callin-recv.o: callin-recv.cpp $(HEADERS) $(DEPS)
	$(CXX) $(CXXFLAGS_ZERO) $(LDFLAGS) -c $< -o $@

callin-recv.lo: callin-recv.cpp $(HEADERS) $(DEPS)
	$(CXX) $(CXXFLAGS_ZERO) $(LDFLAGS) -fPIC -c $< -o $@

libcxxcall.a: $(OBJS)
	rm -f $@
	$(AR) rc $@ $(OBJS)
	$(RANLIB) $@

libcxxcall.so: $(LOBJS)
	$(CXX) -fPIC -shared $(CXXFLAGS) $(LDFLAGS) -o $@ $(LOBJS)

install: $(ALL_LIBS)
	mkdir -p $(includedir)/cxxcall
	cp libcxxcall* $(libdir)/
	cp $(HEADERS) $(includedir)/cxxcall

clean:
	rm -rf *.o *.lo libcxxcall.* tst

